package com.dmjj.cadcommon.page;

import com.github.pagehelper.Page;
import com.github.pagehelper.PageHelper;
import com.github.pagehelper.PageInterceptor;
import org.apache.ibatis.plugin.Interceptor;
import org.springframework.context.annotation.Bean;
import org.springframework.stereotype.Component;

import java.util.Properties;

/**
 * @author 王丽凯
 * @Description: TODO
 * @Title: MybatisPage
 * @ProjectName dmWork
 * @date 2018/12/3
 */
@Component
public class MybatisPage {

    /**
     * @Description: 作用在SqlSessionFactory
     * @return  Interceptor[]
     * @author 王丽凯
     * @date 2018/12/19
     */
    public static Interceptor[] pageHelper(){
        //分页插件
        Interceptor interceptor = new PageInterceptor();
        Properties properties = new Properties();
        //数据库
        properties.setProperty("helperDialect", "sqlserver");
        //是否将参数offset作为PageNum使用
        properties.setProperty("offsetAsPageNum", "true");
        //是否进行count查询
        properties.setProperty("rowBoundsWithCount", "true");
        //是否分页合理化
        properties.setProperty("reasonable", "false");
        interceptor.setProperties(properties);

        //返回结果
        Interceptor[] interceptors = new Interceptor[] {interceptor};
        return interceptors;
    }

    /**
     * @Description: TODO
     * @param  PageNum 页码  PageRow 每页行数
     * @return page
     * @author 王丽凯
     * @date 2018/12/19
     */
    public static Page getPage(Integer PageNum, Integer PageRow){
        int staPage = 0;
        int endPage = 10;
        if (PageNum!=null){
            staPage = PageNum;
        }
        if (PageRow!=null&&PageRow>0){
            endPage = PageRow;
        }
        return PageHelper.startPage(staPage, endPage, true);
    }

    @Bean
    public PageHelper getPageHelper(){
        PageHelper pageHelper = new PageHelper();
        Properties properties = new Properties();
        //数据库
        properties.setProperty("helperDialect", "mysql");
        //是否将参数offset作为PageNum使用
        properties.setProperty("offsetAsPageNum", "true");
        //是否进行count查询
        properties.setProperty("rowBoundsWithCount", "true");
        //是否分页合理化
        properties.setProperty("reasonable", "false");
        pageHelper.setProperties(properties);
        return pageHelper;
    }
}
